package org.softmed.rest.admin;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;

import javax.swing.AbstractButton;
import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.ButtonGroup;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JToggleButton;

public class DetailChooser extends JPanel implements ActionListener {

	public enum DetailLevel {
		MODULE, RESOURCE, HANDLER, METHODS, MEDIA_TYPE
	};

	Map<AbstractButton, DetailLevel> buttonValues = new HashMap<AbstractButton, DetailLevel>();
	Map<DetailLevel, AbstractButton> detailButton = new HashMap<DetailLevel, AbstractButton>();

	private DetailLevel[] detailLevels;
	private boolean toggle;
	DetailLevelListener detailListener;

	int width = 0;
	int height = 20;

	int buttonWidth = 80;
	DetailLevel detailLevel;

	public DetailChooser(DetailLevelListener listener, boolean toggle,
			DetailLevel... detailLevels) {
		setup(toggle, detailLevels);
		this.detailListener = listener;
	}

	public DetailChooser(DetailLevelListener listener, boolean toggle) {
		setup(toggle, DetailLevel.MODULE, DetailLevel.RESOURCE,
				DetailLevel.HANDLER, DetailLevel.METHODS,
				DetailLevel.MEDIA_TYPE);
		this.detailListener = listener;
	}

	public DetailChooser(boolean toggle, DetailLevel... detailLevels) {
		setup(toggle, detailLevels);

	}

	public DetailChooser(boolean toggle) {
		setup(toggle, DetailLevel.MODULE, DetailLevel.RESOURCE,
				DetailLevel.HANDLER, DetailLevel.METHODS,
				DetailLevel.MEDIA_TYPE);
	}

	private void setup(boolean toggle, DetailLevel... detailLevels) {
		this.detailLevels = detailLevels;
		this.toggle = toggle;

		setLayout(new BoxLayout(this, BoxLayout.LINE_AXIS));
//		setBackground(Color.PINK);
		
		
		ButtonGroup group = new ButtonGroup();
		add(new JLabel("View:"));
		for (int i = 0; i < detailLevels.length; i++) {
			DetailLevel detailLevel = detailLevels[i];
			AbstractButton button = null;
			if (toggle) {
				button = new JToggleButton();
				group.add(button);
			} else
				button = new JButton();
			button.setText(getText(detailLevel));
			buttonValues.put(button, detailLevel);
			detailButton.put(detailLevel, button);
			addButton(button);
		}

		add(Box.createHorizontalGlue());

		width = buttonWidth * detailLevels.length + 100;
		setPreferredSize(new Dimension(width, height));
		setMaximumSize(new Dimension(width, height));
		setMinimumSize(new Dimension(width, height));
	}

	// private String getText(DetailLevel detailLevel) {
	// switch (detailLevel) {
	// case RESOURCE:
	// return "Resource";
	//
	// case HANDLER:
	// return "Handler";
	//
	// case MEDIA_TYPE:
	// return "Media Type";
	//
	// case METHODS:
	// return "Methods";
	//
	// case MODULE:
	// return "Module";
	//
	// default:
	// return "INVALID";
	// }
	//
	// }

	private String getText(DetailLevel detailLevel) {
		switch (detailLevel) {
		case RESOURCE:
			return "Resour.";

		case HANDLER:
			return "Handler";

		case MEDIA_TYPE:
			return "Media";

		case METHODS:
			return "Method";

		case MODULE:
			return "Module";

		default:
			return "INVALID";
		}

	}

	protected void addButton(AbstractButton button) {
		resizeButton(button, buttonWidth, height);
		button.addActionListener(this);
		add(button);
	}

	protected void resizeButton(AbstractButton button, int width, int height) {
		button.setPreferredSize(new Dimension(width, height));
		button.setMaximumSize(new Dimension(width, height));
		button.setMinimumSize(new Dimension(width, height));
	}

	@Override
	public void actionPerformed(ActionEvent e) {
		if (detailListener == null)
			return;
		Object source = e.getSource();
		DetailLevel detailLevel = buttonValues.get(source);
		this.detailLevel = detailLevel;
		detailListener.detailLevelChanged(detailLevel);
	}

	public void setDetailLevel(DetailLevel detailLevel) {
		AbstractButton button = detailButton.get(detailLevel);
		button.setSelected(true);
		this.detailLevel = detailLevel;
	}

	public void enable() {
		setEnabled(true);
	}

	public void disable() {
		setEnabled(false);
	}

	public void setEnabled(boolean enabled) {
		Set<AbstractButton> buttons = buttonValues.keySet();
		for (AbstractButton button : buttons) {
			button.setEnabled(enabled);
		}
	}

	public DetailLevelListener getDetailListener() {
		return detailListener;
	}

	public void setDetailListener(DetailLevelListener listener) {
		this.detailListener = listener;
	}

	public DetailLevel getDetailLevel() {
		return detailLevel;
	}

}
